/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Core.Model.Data.Type;
using Parser;

namespace XMHEGDecoder.Builders
{
    public class ObjectRefBuilder
    {
        public static ObjectRef Build ( XmlElement node )
        {
            ObjectRef ret = new ObjectRef();

            XmlElement reference;
            if ( node.Name == "external-reference" )
                reference = node;
            else
                reference = node ["external-reference"];

            if ( reference != null )
            {
                XmlElement group = reference ["group-identifier"];
                XmlElement num = reference ["object-number"];

                if ( group != null && num != null )
                {
                    ret.GroupIdentifier = DecodeUtils.ParseStringFromNode( group);
                    ret.ObjectNumber = DecodeUtils.ParseInt( num.InnerText );
                }
            }
            else if ( ( reference = node ["internal-reference"] ) != null )
            {
                ret.ObjectNumber = DecodeUtils.ParseInt( reference.InnerText );
                                   
                 XmlNode x =  node.OwnerDocument.GetElementsByTagName("scene")[0];
                if (x == null){
                    x =  node.OwnerDocument.GetElementsByTagName("application")[0];
                }
                if (x == null)
                    throw new Exception("Can't find parent app / scene");

                ret.GroupIdentifier = DecodeUtils.ParseStringFromNode( ( XmlElement ) x.FirstChild.FirstChild.FirstChild );
            }
            else if ( node.Name == "internal-reference" )
            {
                ret.ObjectNumber = DecodeUtils.ParseInt( node.InnerText );

                XmlNode x =  node.OwnerDocument.GetElementsByTagName( "scene" ) [0];
                if ( x == null )
                {
                    x =  node.OwnerDocument.GetElementsByTagName( "application" ) [0];
                }
                if ( x == null )
                    throw new Exception( "Can't find parent app / scene" );

                ret.GroupIdentifier = DecodeUtils.ParseStringFromNode( ( XmlElement ) x.FirstChild.FirstChild.FirstChild );


            }
            else
                throw new Exception( "Unkown object ref" );

            return ret;
        }

    }
}
